﻿using System;
using System.Windows;
using Vtron.ATSP.BusinessLogic;
using Vtron.ATSP.Infrastructure;
using System.Windows.Input;
using System.Drawing;
using Vtron.ATSP.DomainObjects;
using System.Windows.Controls;
using System.Collections.Generic;

namespace Vtron.ATSP.WpfApp
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        ScreenTestHandler handler = ScreenTestHandlerFactory.Instance.ScreenTestHandler;
        Color color = Color.White;

        private bool connectProjector()
        {
            bool result = false;
            try
            {
                result = handler.ProjectorCheckConnect();
            }
            catch (Exception ex)
            {
                MessageBox.Show(String.Format("投影机连接失败！错误信息：{0}", ex.Message));
                return false;
            }

            if (!result)
            {
                MessageBox.Show("投影机连接失败！");
            }

            return result;
        }

        private void startProjector_btn_Click(object sender, RoutedEventArgs e)
        {
            SetProjectorIP();

            if (!connectProjector())
            {
                return;
            }

            bool result = false;

            try
            {
                result = handler.ProjectorStart();
            }
            catch (Exception ex)
            {
                MessageBox.Show(String.Format("投影机开机失败！错误信息：{0}", ex.Message));
                return;
            }

            if (result)
            {
                MessageBox.Show("投影机开机成功！");
            }
            else
            {
                MessageBox.Show("投影机开机失败！");
            }
        }

        private void closeProjector_btn_Click(object sender, RoutedEventArgs e)
        {
            SetProjectorIP();

            if (!connectProjector())
            {
                return;
            }

            bool result = false;

            try
            {
                result = handler.ProjectorAwait();
            }
            catch (Exception ex)
            {
                MessageBox.Show(String.Format("投影机开机失败！错误信息：{0}", ex.Message));
                return;
            }

            if (result)
            {
                MessageBox.Show("投影机开机成功！");
            }
            else
            {
                MessageBox.Show("投影机开机失败！");
            }
        }

        private void testerConnect_btn_Click(object sender, RoutedEventArgs e)
        {
            SetProjectorIP();

            if (!connectProjector())
            {
                return;
            }

            Utils.TesterPortName = testerPortName_ComboBox.SelectionBoxItem.ToString();
            Utils.TesterBaudRate = Int32.Parse(testerBaudRate_ComboBox.SelectionBoxItem.ToString());

            bool result = false;
            try
            {
                result = handler.SerialCheckCOM();
            }
            catch (Exception ex)
            {
                MessageBox.Show(String.Format("测试仪连接失败！错误信息：{0}", ex.Message));
                return;
            }

            if (result)
            {
                connect_grid.Visibility = Visibility.Collapsed;
                functionChoise_grid.Visibility = Visibility.Visible;
                adjust_grid.Visibility = Visibility.Collapsed;
                measurement_grid.Visibility = Visibility.Collapsed;
            }
            else
            {
                MessageBox.Show("测试仪连接失败！");
            }
        }

        private void SetProjectorIP()
        {
            string projectIp = projectorIp_TextBox.Text;
            Utils.ProjectorIP = projectIp;
        }

        private void adjustFunction_btn_Click(object sender, RoutedEventArgs e)
        {
            connect_grid.Visibility = Visibility.Collapsed;
            functionChoise_grid.Visibility = Visibility.Collapsed;
            adjust_grid.Visibility = Visibility.Visible;
            measurement_grid.Visibility = Visibility.Collapsed;
            Title = "传感器校准";
            color = Color.White;
            SetDesktop(color);
        }

        private void measurementFunction_btn_Click(object sender, RoutedEventArgs e)
        {
            connect_grid.Visibility = Visibility.Collapsed;
            functionChoise_grid.Visibility = Visibility.Collapsed;
            adjust_grid.Visibility = Visibility.Collapsed;
            measurement_grid.Visibility = Visibility.Visible;
            Title = "屏幕参数测量";
            color = Color.White;
            SetDesktop(color);
        }

        private void SetDesktop(Color color)
        {
            bool result = false;
            try
            {
                result = handler.SetDesktop(color);
            }
            catch (Exception ex)
            {
                MessageBox.Show(String.Format("设置桌面失败！错误信息：{0}", ex.Message));
                return;
            }

            if (!result)
            {
                MessageBox.Show("设置桌面失败！");
            }
        }

        private void white_Border_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            color = Color.White;
            SetDesktop(color);
        }

        private void red_Border_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            color = Color.Red;
            SetDesktop(color);
        }

        private void green_Border_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            color = Color.Green;
            SetDesktop(color);
        }

        private void blue_Border_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            color = Color.Blue;
            SetDesktop(color);
        }

        private void setADC_btn_Click(object sender, RoutedEventArgs e)
        {
            Range range = new Range();
            range.DeviceNum = byte.Parse(deviceNum_ComboBox.SelectionBoxItem.ToString());
            range.ReferCurrent = byte.Parse((referCurrent_ComboBox.SelectedItem as ComboBoxItem).Tag.ToString());
            range.IntegrationPeriod = byte.Parse((integrationPeriod_ComboBox.SelectedItem as ComboBoxItem).Tag.ToString());

            bool result = false;

            try
            {
                result = handler.SetADC(range, color);
            }
            catch (Exception ex)
            {
                MessageBox.Show(String.Format("设置ADC参数失败！错误信息：{0}", ex.Message));
            }

            if (result)
            {
                MessageBox.Show("设置ADC参数成功！");
            }
            else
            {
                MessageBox.Show("设置ADC参数失败！");
            }
        }

        private void getSensor_btn_Click(object sender, RoutedEventArgs e)
        {
            ushort result = 0;
            byte deviceNum = byte.Parse(deviceNum_ComboBox.SelectionBoxItem.ToString());

            try
            {
                result = handler.GetSensor(deviceNum);
            }
            catch (Exception ex)
            {
                MessageBox.Show(String.Format("获取ADC-SENSOR数据失败！错误信息：{0}", ex.Message));
            }

            collectionValue_TextBlock.Text = result.ToString();
        }

        private void adjust_btn_Click(object sender, RoutedEventArgs e)
        {
            int white = 0;
            int red = 0;
            int green = 0;
            int blue = 0;
            int black = 0;

            if (!Int32.TryParse(white_TextBox.Text, out white))
            {
                MessageBox.Show("输入有误！请输入有效的白色校准仪器亮度值。");
                return;
            }
            if (!Int32.TryParse(red_TextBox.Text, out red))
            {
                MessageBox.Show("输入有误！请输入有效的红色校准仪器亮度值。");
                return;
            }
            if (!Int32.TryParse(green_TextBox.Text, out green))
            {
                MessageBox.Show("输入有误！请输入有效的绿色校准仪器亮度值。");
                return;
            }
            if (!Int32.TryParse(blue_TextBox.Text, out blue))
            {
                MessageBox.Show("输入有误！请输入有效的蓝色校准仪器亮度值。");
                return;
            }
            if (!Int32.TryParse(black_TextBox.Text, out black))
            {
                MessageBox.Show("输入有误！请输入有效的黑色校准仪器亮度值。");
                return;
            }

            CalibrationInstrumentBrightness brightness = new CalibrationInstrumentBrightness();
            brightness.White = white;
            brightness.Red = red;
            brightness.Green = green;
            brightness.Blue = blue;
            brightness.Black = black;
            brightness.DeviceNum = byte.Parse(deviceNum_ComboBox.SelectionBoxItem.ToString());

            try
            {
                handler.Adjust(brightness);
            }
            catch (Exception ex)
            {
                MessageBox.Show(String.Format("校准错误！错误信息：{0}", ex.Message));
                return;
            }

            MessageBox.Show("校准成功！");
        }

        private void measurement_btn_Click(object sender, RoutedEventArgs e)
        {
            int white = 0;
            int red = 0;
            int green = 0;
            int blue = 0;

            if (!Int32.TryParse(whiteMeasurement_TextBox.Text, out white))
            {
                MessageBox.Show("输入有误！请输入有效的白色参考亮度值。");
                return;
            }
            if (!Int32.TryParse(redMeasurement_TextBox.Text, out red))
            {
                MessageBox.Show("输入有误！请输入有效的红色参考亮度值。");
                return;
            }
            if (!Int32.TryParse(greenMeasurement_TextBox.Text, out green))
            {
                MessageBox.Show("输入有误！请输入有效的绿色参考亮度值。");
                return;
            }
            if (!Int32.TryParse(blueMeasurement_TextBox.Text, out blue))
            {
                MessageBox.Show("输入有误！请输入有效的蓝色参考亮度值。");
                return;
            }

            ReferenceBrightness brightness = new ReferenceBrightness();
            brightness.White = white;
            brightness.Red = red;
            brightness.Green = green;
            brightness.Blue = blue;

            TestResultsCollection results;
            try
            {
                results = handler.Measure(brightness);
            }
            catch (Exception ex)
            {
                MessageBox.Show(String.Format("测试错误！错误信息：{0}", ex.Message));
                return;
            }

            //TODO:

            MessageBox.Show("测试成功！");
        }

        private void linearityTest_btn_Click(object sender, RoutedEventArgs e)
        {
            ushort spacingCurrent = 0;

            if (!UInt16.TryParse(spacingCurrent_TextBox.Text, out spacingCurrent))
            {
                MessageBox.Show("输入有误！请输入有效的白色参考亮度值。");
                return;
            }

            byte deviceNum = byte.Parse(deviceNum_ComboBox.SelectionBoxItem.ToString());

            Dictionary<ushort, ushort> results;
            try
            {
                results = handler.LinearityMeasure(deviceNum, spacingCurrent);
            }
            catch (Exception ex)
            {
                MessageBox.Show(String.Format("测试错误！错误信息：{0}", ex.Message));
                return;
            }

            //TODO:
        }

        private void repeatabilityTest_btn_Click(object sender, RoutedEventArgs e)
        {
            int collectionTimes = 0;

            if (!Int32.TryParse(collectionTimes_TextBox.Text, out collectionTimes))
            {
                MessageBox.Show("输入有误！请输入有效的白色参考亮度值。");
                return;
            }

            byte deviceNum = byte.Parse(deviceNum_ComboBox.SelectionBoxItem.ToString());

            Dictionary<int, ushort> results;
            try
            {
                results = handler.RepeatabilityMeasure(deviceNum, collectionTimes);
            }
            catch (Exception ex)
            {
                MessageBox.Show(String.Format("测试错误！错误信息：{0}", ex.Message));
                return;
            }

            //TODO:
        }
    }
}
